package com.itzyf.pokemondata.activity

import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.DiscountDo
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_discount.*
import java.lang.reflect.Type

/**
 * @author 依风听雨
 * @version 创建时间：2019/02/03 22:17
 */
class DiscountActivity : BaseActivity() {
    private val itemsDiscount = arrayOf("5.5折", "6.5折", "8折", "9折", "9.8折", "10折")
    private val itemsBreakthrough = arrayOf(
        "白", "绿", "绿1", "绿2", "蓝", "蓝1",
        "蓝2", "紫", "紫1", "紫2", "橙", "橙1", "橙2", "橙3", "红", "红1"
    )

    private lateinit var discountList: List<List<DiscountDo>>
    private var discountIndex = 0
    private var discountStartIndex = 0
    private var discountEndIndex = 0
    override fun handlerActivity() {
        discountList = readJson(
            R.raw.resourse,
            object : TypeToken<List<List<DiscountDo>>>() {}.type
        )

        spDiscount.setItems(itemsDiscount.toList())
        spStart.setItems(itemsBreakthrough.toList())
        spEnd.setItems(itemsBreakthrough.toList())

        spDiscount.setOnItemSelectedListener { _, position, _, _ ->
            discountIndex = position
            calculate()
        }
        spStart.setOnItemSelectedListener { _, position, _, _ ->
            discountStartIndex = position
            calculate()
        }
        spEnd.setOnItemSelectedListener { _, position, _, _ ->
            discountEndIndex = position
            calculate()
        }

        calculate()
    }


    private fun calculate() {
        val res = DiscountDo()
        when {
            discountStartIndex > discountEndIndex -> showToast("突破前必须大于等于突破后")
            discountStartIndex == discountEndIndex -> {
            }
            else -> for (i in (discountStartIndex + 1)..discountEndIndex) {
                val discountDo = discountList[discountIndex][i]
                res.jingHua += discountDo.jingHua
                res.jieJing += discountDo.jieJing
                res.shiDiao += discountDo.shiDiao
                res.hunyu += discountDo.hunyu
                res.zhuishi += discountDo.zhuishi
                res.yueshi += discountDo.yueshi
                res.hunyu2 += discountDo.hunyu2
                res.jingHua2 += discountDo.jingHua2
                res.wanou += discountDo.wanou
                res.money += discountDo.money
            }
        }

        llRoot.removeAllViews()
        createTextView(R.drawable.jing_hua, "蜕变精华", res.jingHua)
        createTextView(R.drawable.jiejing, "玩偶结晶", res.jieJing)
        createTextView(R.drawable.shi_diao, "精灵石雕", res.shiDiao)

        if (res.hunyu > 0) {
            createTextView(R.drawable.hunyu, "蜕变魂玉", res.hunyu)
            createTextView(R.drawable.zhuishi, "玩偶坠饰", res.zhuishi)
            createTextView(R.drawable.yueshi, "传说钥石", res.yueshi)
        }
        if (res.hunyu2 > 0) {
            createTextView(R.drawable.hunyu2, "极限魂玉", res.hunyu2)
            createTextView(R.drawable.jing_hua2, "琥珀精华", res.jingHua2)
            createTextView(R.drawable.wanou, "奇迹玩偶", res.wanou)
        }
        createTextView(R.drawable.money, "所需金币", res.money)
    }


    private fun createTextView(@DrawableRes resId: Int, text: String, value: Int) {
        val view = layoutInflater.inflate(R.layout.item_text_data2, null)
        view.findViewById<ImageView>(R.id.icon).setImageResource(resId)
        view.findViewById<TextView>(R.id.text).text = text
        view.findViewById<TextView>(R.id.tvDiscount).text = "$value"
        if ("所需金币" == text) {
            view.findViewById<TextView>(R.id.tvDiscount).text = "${value}万"
            view.findViewById<View>(R.id.line).visibility = View.GONE
        }
        llRoot.addView(view)
    }

    override fun getContentViewId() = R.layout.activity_discount

    override fun getToolbarTitle() = "突破资源计算"


    data class redDo(val material1: Int, val material2: Int, val material3: Int)


    private fun <T> readJson(raw: Int, type: Type): T {
        val stream = resources.openRawResource(raw)
        return stream.buffered().reader().use { reader -> Gson().fromJson(reader.readText(), type) }
    }
}